package com.eric.jpa;

import com.eric.jpa.dao.CompanyRepository;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.concurrent.CountDownLatch;

/**
 * @author chenw-i
 * @version 1.0.0
 * @ClassName Application.java
 * @Description 这是一个java 本地应用，用户集成springboot 的运行环
 * @createTime 2021年04月23日 14:34:00
 */
@SpringBootApplication
public class Application implements ApplicationContextAware {
    public static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;

        CompanyRepository companyRepository = context.getBean(CompanyRepository.class);
        long count = companyRepository.count();
        System.out.println("size ： " + count);

    }

    /**
     * springboot 作为一个本地运行的应用程序需要在相关加载完成之后完成一个额外的动作
     * 需要将程序执行完成之后挂起
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class,args);
        // 挂起当前线程
        new CountDownLatch(1).await();
    }
}
